package com.xc.freeapp.account.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Issue;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.IssueService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

@Controller
@RequestMapping("/issue")
@Api(value="/issue",description = "就诊记录")
public class IssueController extends AbstractController {
	@Autowired
	private IssueService issueService;

	/**
	 * 根究关系id获取就诊记录列表
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/byRelationId", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根究关系id获取就诊记录列表")
	public List<Issue> getIssueListByRelationId(@RequestParam("relationId") String relationId)
			throws BaseException {
		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, relationId,
				"relationId");
		log.info("根据关系id" + relationId + "获取列表");
		PageBounds pageBounds = new PageBounds(0, 100);
		List<Issue> issues = issueService.getList(relationId,pageBounds);
		for (Issue issue : issues) {
			issue.setPiclist(UploadUtil.pathstoList(issue.getPics()));
		}
		return issues;
	}
	/**
	 * 保存就诊记录
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存就诊记录")
	public Issue saveIssue(@RequestBody @Valid Issue issue)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		issue.setPics(UploadUtil.uploads(issue.getPiclist(),authInfo.getAppIntId()));
		CheckUtils.notNull(Constant.VER_NOTNULL, issue);
		log.info("保存就诊记录");
		issue.setUpdateby(authInfo.getUserIntId());
		issue.setCreateby(authInfo.getUserIntId());
		issueService.insert(issue);
		Issue responseissue = issueService.selectByPrimaryKey(issue.getId());
		responseissue.setPiclist(UploadUtil.pathstoList(responseissue.getPics()));
		return responseissue;
	}
	/**
	 * 更新就诊记录
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "更新就诊记录")
	public Issue updatessue(@RequestBody Issue issue)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		issue.setPics(UploadUtil.uploads(issue.getPiclist(),authInfo.getAppIntId()));
		CheckUtils.notNull(Constant.VER_NOTNULL, issue);
		log.info("保存就诊记录");
		issue.setUpdateby(authInfo.getUserIntId());
		issueService.updateByPrimaryKey(issue);
		Issue responseissue = issueService.selectByPrimaryKey(issue.getId());
		responseissue.setPiclist(UploadUtil.pathstoList(responseissue.getPics()));
		return responseissue;
	}


	/**
	 * 删除就诊记录
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	@ApiOperation(value = "删除就诊记录")
	public ResponseBean deleteIssue(@PathVariable("id") String id) throws BaseException {
		
		log.info("删除关系记录");
		int del = issueService.deleteByPrimaryKey(Integer.parseInt(id));
		if (del > 0) {
			return ResponseBean.getSuccess();
		} else {
			throw new BaseException(Constant.COMMON_ERROR);
		}
	}

	/**
	 * 根据ID查询就诊记录
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据ID查询就诊记录")
	public Issue selectIssueById(@PathVariable("id") String id) throws BaseException {

		Issue issue = issueService.selectByPrimaryKey(Integer.valueOf(id));
		if(issue!=null){
			issue.setPiclist(UploadUtil.pathstoList(issue.getPics()));
		}
		return issue;
	}

}
